﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2010
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System.Text.RegularExpressions
Imports System.Web.UI.WebControls
Imports DotNetNuke.Services.FileSystem

Namespace DotNetNuke.Modules.Media

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The EditImage PortalModuleBase is used to manage the Image
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[lpointer]	31.10.2005	Moved Images to a separate Project
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Partial Public MustInherit Class EditMedia
        Inherits MediaModuleBase

#Region " Private Members "

        Protected p_isNew As Boolean = True
        Dim p_UseModuleSettings As String = Null.NullString
        Dim p_None As String = Null.NullString
        Dim p_Left As String = Null.NullString
        Dim p_Center As String = Null.NullString
        Dim p_Right As String = Null.NullString
        Dim p_MEDIA_FILE_TYPES As String = Null.NullString

#End Region

#Region " Properties "

        ''' <summary>
        ''' MEDIA_FILE_TYPES - in conjunction with the DNN global images, these are the file types that this module will work with.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>
        ''' This property had to be created to replace the constant, because DNN 5.x handles the file extensions differently than 4.x.
        ''' </remarks>
        ''' <history>
        ''' [wstrohl] - 20100523 - Created from a constant.
        ''' </history>
        Private ReadOnly Property MEDIA_FILE_TYPES() As String
            Get
                If Not String.IsNullOrEmpty(Me.p_MEDIA_FILE_TYPES) Then
                    Return Me.p_MEDIA_FILE_TYPES
                End If

                If DotNetNuke.Common.Globals.glbAppVersion.StartsWith("04.") Then
                    Me.p_MEDIA_FILE_TYPES = "swf,avi,wmv,midi,wav,asx,mp3,mpeg,asf,wma,ram,rpm,rm,mov,qt"
                Else
                    Me.p_MEDIA_FILE_TYPES = ".swf,.avi,.wmv,.midi,.wav,.asx,.mp3,.mpeg,.asf,.wma,.ram,.rpm,.rm,.mov,.qt"
                End If

                Return Me.p_MEDIA_FILE_TYPES
            End Get
        End Property

#End Region

#Region " Event Handlers "

        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                'Get the IsNew state from the ViewState
                p_isNew = Convert.ToBoolean(ViewState("IsNew"))

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If
                'Save the IsNew state to the ViewState
                ViewState("IsNew") = p_isNew

            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        ''' <summary>
        ''' cmdCancel_Click runs when the cancel button is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click

            Me.SendBackToModule()

        End Sub

        ''' <summary>
        ''' cmdUpdate_Click runs when the update button is clicked
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click

            Me.SaveMedia()
            Me.SendBackToModule()

        End Sub

#End Region

#Region " Private Methods "

        Private Sub BindData()

            Me.LocalizeModule()

            ' Obtain a single row of text information
            Dim objMediaController As New MediaController
            Dim objMedia As MediaInfo = objMediaController.GetMedia(ModuleId)

            ' Load the Alignment list with localised values
            With ddlImageAlignment.Items
                .Add(New ListItem(p_UseModuleSettings, "0"))
                .Add(New ListItem(p_None, "1"))
                .Add(New ListItem(p_Left, "2"))
                .Add(New ListItem(p_Center, "3"))
                .Add(New ListItem(p_Right, "4"))
            End With

            ctlURL.FileFilter = String.Concat(glbImageFileTypes, ",", MEDIA_FILE_TYPES)
            With ctlNavigateUrl
                .ShowNewWindow = True
                .ShowTrack = True
                .ShowTabs = True
                .ShowFiles = True
            End With

            If Not objMedia Is Nothing AndAlso objMedia.ModuleID > Null.NullInteger Then
                With objMedia
                    p_isNew = False
                    Me.ddlImageAlignment.SelectedValue = .MediaAlignment.ToString
                    Me.ctlURL.Url = .Src
                    Me.txtAlt.Text = .Alt
                    If .Width <> Null.NullInteger Then
                        Me.txtWidth.Text = .Width.ToString
                    End If
                    If .Height <> Null.NullInteger Then
                        Me.txtHeight.Text = .Height.ToString
                    End If
                    Me.ctlNavigateUrl.Url = .NavigateUrl
                    Me.ctlTracking.URL = .NavigateUrl
                    Me.ctlTracking.ModuleID = ModuleId
                    Me.chkAutoStart.Checked = objMedia.AutoStart
                    Me.chkLoop.Checked = objMedia.MediaLoop
                End With
            Else
                p_isNew = True
            End If

        End Sub

        Private Sub LocalizeModule()
            p_UseModuleSettings = Me.GetLocalizedString("UseModuleSettings.Text")
            p_None = Me.GetLocalizedString("None.Text")
            p_Left = Me.GetLocalizedString("Left.Text")
            p_Center = Me.GetLocalizedString("Center.Text")
            p_Right = Me.GetLocalizedString("Right.Text")

            Me.lblImagesOnly.Text = Me.GetLocalizedString("lblImagesOnly.Text")
            Me.lblVideosOnly.Text = Me.GetLocalizedString("lblVideosOnly.Text")
        End Sub

        Private Sub SaveMedia()

            Dim objMediaController As New MediaController
            Dim objMedia As New MediaInfo

            Try
                ' Update settings in the database
                If String.Equals(ctlURL.UrlType, "F") Then
                    Dim ctlFiles As New FileController
                    Dim objFile As FileInfo = ctlFiles.GetFileById( _
                        Integer.Parse(Regex.Match(Me.ctlURL.Url, "\d+").Value, Globalization.NumberStyles.Integer), PortalId)
                    If Not objFile Is Nothing Then
                        If String.IsNullOrEmpty(Me.txtWidth.Text) Then
                            Me.txtWidth.Text = objFile.Width.ToString
                        End If
                        If String.IsNullOrEmpty(Me.txtHeight.Text) Then
                            Me.txtHeight.Text = objFile.Height.ToString
                        End If
                    End If
                End If

                With objMedia
                    .ModuleID = ModuleId
                    .Src = Me.ctlURL.Url
                    .Alt = Me.txtAlt.Text
                    If Not String.IsNullOrEmpty(Me.txtWidth.Text) Then
                        .Width = Integer.Parse(Me.txtWidth.Text, Globalization.NumberStyles.Integer)
                    End If
                    If Not String.IsNullOrEmpty(Me.txtHeight.Text) Then
                        .Height = Integer.Parse(Me.txtHeight.Text, Globalization.NumberStyles.Integer)
                    End If
                    .NavigateUrl = Me.ctlNavigateUrl.Url
                    .MediaAlignment = Integer.Parse(Me.ddlImageAlignment.SelectedValue, Globalization.NumberStyles.Integer)
                    .AutoStart = Me.chkAutoStart.Checked
                    .MediaLoop = Me.chkLoop.Checked
                End With

                ' url tracking
                Dim objUrls As New UrlController
                objUrls.UpdateUrl(PortalId, Me.ctlNavigateUrl.Url, Me.ctlNavigateUrl.UrlType, Me.ctlNavigateUrl.Log, _
                    Me.ctlNavigateUrl.Track, ModuleId, Me.ctlNavigateUrl.NewWindow)

                ' add/update
                If p_isNew Then
                    objMediaController.AddMedia(objMedia)
                Else
                    objMediaController.UpdateMedia(objMedia)
                End If

            Catch exc As Exception    'Module failed to load
                LogException(exc)
                'ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        Private Sub SendBackToModule()

            Try
                Response.Redirect(NavigateURL(), True)
            Catch
                ' do nothing
            End Try

        End Sub

#End Region

    End Class

End Namespace